package NLP;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import sentStruct.Noun;
import sentStruct.PartOfSpeech;
import sentStruct.Sentence;
import sentStruct.Verb;
import edu.stanford.nlp.ling.CoreAnnotations.LemmaAnnotation;
import edu.stanford.nlp.ling.CoreAnnotations.PartOfSpeechAnnotation;
import edu.stanford.nlp.ling.CoreAnnotations.SentencesAnnotation;
import edu.stanford.nlp.ling.CoreAnnotations.TextAnnotation;
import edu.stanford.nlp.ling.CoreAnnotations.TokensAnnotation;
import edu.stanford.nlp.ling.CoreLabel;
import edu.stanford.nlp.pipeline.Annotation;
import edu.stanford.nlp.pipeline.StanfordCoreNLP;
import edu.stanford.nlp.util.CoreMap;

public class SentenceCreator {
	
	
	
	public List<Sentence> createSentences(String text){
		
		
	    StanfordCoreNLP pipeline = StanfordPipeline.getInstance();
	    List<Sentence> listSentences= new ArrayList<Sentence>();
		
	    Annotation document = new Annotation(text);
	    
	    // run all Annotators on this text
	    pipeline.annotate(document);
	    
	    // these are all the sentences in this document
	    // a CoreMap is essentially a Map that uses class objects as keys and has values with custom types
	    List<CoreMap> sentences = document.get(SentencesAnnotation.class);
	    
	    for(CoreMap sentenceAnnotation: sentences) {
	    	Sentence sentInstance = new Sentence();
	    	sentInstance.setText(sentenceAnnotation.get(TextAnnotation.class));
	    	setElementsIntoSentence(sentenceAnnotation, sentInstance);
                
	    	listSentences.add(sentInstance);
	    	
		
	}
		return listSentences;
	    
	}

	private void setElementsIntoSentence(CoreMap sentenceAnnotation,
			Sentence sentInstance) {
		setTokensIntoSentence(sentenceAnnotation, sentInstance);
		setSubjectsIntoSentence(sentenceAnnotation , sentInstance);
		setPredicateIntoSentence(sentenceAnnotation , sentInstance);
		setObjectIntoSentence(sentenceAnnotation, sentInstance);
		setPrepositionObjIntoSentence(sentenceAnnotation, sentInstance);
	}

	private void setPrepositionObjIntoSentence(CoreMap sentenceAnnotation,
			Sentence sentInstance) {
		SentenceElementsExtractor extractor = new SentenceElementsExtractor(sentenceAnnotation);
		List<Noun> listPRObj = extractor.extractPrepositionObjects();
		sentInstance.setPrepositionObjects(listPRObj);
	}

	private void setObjectIntoSentence(CoreMap sentenceAnnotation, Sentence sentInstance) {
		SentenceElementsExtractor extractor = new SentenceElementsExtractor(sentenceAnnotation);
		List<Noun> listObjects = extractor.extractObjects();
		sentInstance.setObjects(listObjects);
		
	}

	private void setPredicateIntoSentence(CoreMap sentenceAnnotation, Sentence sentInstance) {
		SentenceElementsExtractor extractor = new SentenceElementsExtractor(sentenceAnnotation);
		List<Verb> listPredicates = extractor.extractPredicates();
		sentInstance.setVerbs(listPredicates);
		
	}

	private void setSubjectsIntoSentence(CoreMap sentenceAnnotation, Sentence sentInstance) {
		SentenceElementsExtractor extractor = new SentenceElementsExtractor(sentenceAnnotation);
		List<Noun> listSubjects = extractor.extractSubjects();
		sentInstance.setSubjects(listSubjects);
	}

	

	private void setTokensIntoSentence(CoreMap sentence, Sentence sent) {
		
		PartOfSpeechAnalyser POSanalyser = new PartOfSpeechAnalyser();
		for (CoreLabel token: sentence.get(TokensAnnotation.class)) {
      
      
		// this is the POS tag of the token
    
		String word = token.get(LemmaAnnotation.class);
		String pos = token.get(PartOfSpeechAnnotation.class);
		System.out.println("(SYM:"+pos+")");
		if (!pos.equals("SYM")&&!pos.matches("\\W*")){ //take off symbols like .;,?/
		Class<?> clss = POSanalyser.stringToClass(pos);
		PartOfSpeech partOfspeech = null;
		try {
			partOfspeech =  (PartOfSpeech) clss.newInstance();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		partOfspeech.setValue(word);
		sent.addToken(partOfspeech);
		}
		
		
     }
	}
	
	
}
